package cn.kgc.springmvc.day02.controller;

import cn.kgc.springmvc.day02.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @author 课工场
 * @date 2024/5/6
 * @description
 *  @RequestMapping
 *  1. 默认情况下 该注解可以接收  get  post  put  delete等请求方式
 *  2.如果想指定@RequestMapping只处理某种请求方式  可以通过注解属性 method  = { }设置
 *  3.可以通过设置value/path属性  实现多路径匹配
 *
 *  springmvc如何实现参数接收
 *    1.基本类型参数
 *         1.1可以直接在方法的参数列表中直接声明需要接收的参数 形式参数的名字必须保持和请求对象中的参数的key一致
 *         1.2 使用基本类型实现参数的接收，参数必须传递 否则会抛出异常    int  age = null
 *             @RequestParam
 *                1.参数在请求对象中已经传递 但是和声明的形式传参数不一致 可以通过该注解指定
 *                2. 使用该注解修饰的参数 必须按照注解中指定的名称进行传递  否则找不到参数会抛出异常
 *
 *    2.包装类型参数
 *    3.对象类型参数        请求对象中参数的key要保持和对象中的属性名一致  否则封装失败
 *    4.数组类型参数
 *    5.json格式数据的接收   json是一种轻量级的数据传输格式  独立与任何一门语言    xml
 */
@Controller
@RequestMapping("param")
public class ParamsController {


    @RequestMapping(value = {"m1","method1"},method ={RequestMethod.GET,RequestMethod.POST} )
    public ModelAndView method1() {

        System.out.println("hello  springmvc");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("info","参数的接收");
        return modelAndView;
    }

    // get   localhost:8080/mvc/param/m2?username=tom&password=123456&age=20&sex=1
    @RequestMapping("m2")
    public ModelAndView method2(@RequestParam(value = "uname",required = false) String username, String password, @RequestParam("age") int age, Integer sex) {


        System.out.println("username = " + username + ", password = " + password + ", age = " + age + ", sex = " + sex);


        System.out.println("hello  springmvc");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("info","参数的接收");
        return modelAndView;
    }

    // get   localhost:8080/mvc/param/m2?username=tom&password=123456&age=20&sex=1
    @RequestMapping("m3")
    public ModelAndView method3(User user) {

        System.out.println("user = " + user);

        System.out.println("hello  springmvc");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("info","参数的接收");
        return modelAndView;
    }

    // get   localhost:8080/mvc/param/m4?id=1&id=2&id=3
    @RequestMapping("m4")
    public ModelAndView method4(Integer[]  id) {

        Arrays.asList(id).forEach(System.out::println);

        System.out.println("hello  springmvc");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("info","参数的接收");
        return modelAndView;
    }


    // json  @RequestBody
    @RequestMapping("m5")
    public ModelAndView method5(@RequestBody User user) {

        System.out.println("user = " + user);

        System.out.println("hello  springmvc");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("info","参数的接收");
        return modelAndView;
    }

    @RequestMapping("m6")
    public ModelAndView method6(HttpServletRequest request, HttpServletResponse response) {

        String username = request.getParameter("username");


        System.out.println("hello  springmvc");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("info","参数的接收");
        return modelAndView;
    }
}
